package com.xm;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.xm.common.HttpSingleton;
import com.xm.entity.SearchAppResponse;

import java.io.IOException;
import java.net.URI;
import java.net.http.HttpClient;
import java.net.http.HttpRequest;
import java.net.http.HttpResponse;

/**
 * @Author tianqin
 * @Date 2024/7/26 15:44
 * 执行业务主流程
 */
public class BuyMain {

    public static void main(String[] args) throws IOException, InterruptedException {
        // 假设你已经有HttpClient单例模式获取的方法 HttpSingleton.getInstance()
        HttpClient httpClient = HttpSingleton.getInstance();

        String name = "越王宝剑";
        // 构建请求体字符串
        String requestBody = String.format("{\"platformIds\":[],\"pageNum\":1,\"type\":\"0\",\"search\":\"%s\",\"isTransfer\":null,\"goodsTypeList\":[2,3]}",name);

        HttpRequest request = HttpRequest.newBuilder()
                .uri(URI.create("https://api.x-metash.cn/h5/home/searchApp"))
                .header("Content-Type", "application/json")
                .POST(HttpRequest.BodyPublishers.ofString(requestBody))
                .build();

        // 发送请求并接收响应
        HttpResponse<String> response = httpClient.send(request, HttpResponse.BodyHandlers.ofString());

        // 使用Jackson ObjectMapper来解析JSON响应体
        ObjectMapper objectMapper = new ObjectMapper();
        // 请确保使用与API响应相匹配的类代替以下的 SearchAppEntity
        SearchAppResponse searchAppResponse = objectMapper.readValue(response.body(),SearchAppResponse.class);
        System.out.println(searchAppResponse);
    }
}

